package com.example.hms.ulit;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class TokenUtil {
    //密钥
    static String TOKENS = "sssadaa";
    //加密
    public static String getToken(String userId, String userName, String roleName, long time){
        String token = JWT.create()
                .withClaim("userId",userId)
                .withClaim("roleName",roleName)
                .withClaim("userName",userName)
                .withClaim("time",time)
                .sign(Algorithm.HMAC256(TOKENS));
        return token;

    }
    //解密
    public static Map<String, Object> checkToken(String token){
        Map<String,Object> map = new HashMap<>();
        DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(TOKENS)).build().verify(token);
        Claim userId =decodedJWT.getClaim("userId");
        Claim roleName =decodedJWT.getClaim("roleName");
        Claim userName =decodedJWT.getClaim("userName");
        Claim time =decodedJWT.getClaim("time");
        map.put("userId",userId.asString());
        map.put("userName",userName.asString());
        map.put("roleName",roleName.asString());
        map.put("time",time.asLong());
        return map;
    }



}
